﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using ClassLib.Classes.Common;
using ClassLib.UserControl;
using DevExpress.XtraReports.UI;
using ISumen;
using Sumen.Business;
using Sumen.Business.Model;
using Sumen.Business.Model.Presenter.Sales;
using Sumen.Business.Model.Presenter.Sys;
using Sumen_Stock.LookAndFeel.Report.Sales;
using System.IO;

namespace Sumen_Stock.LookAndFeel.Sales
{
    public partial class Frm_Confirm_Sales : DevExpress.XtraEditors.XtraForm
    {
        private FromCode _fromCode = FromCode.FrmSale;
        private EnumFormStatus _formStatus = EnumFormStatus.Add;

        public rptConfirmSales rptConfirm;
        private OrdersPresenter _ordersPresenter;

        string urlImageDes = ClassLib.Classes.Connection.ClsConnection.LocationSave;

        public Frm_Confirm_Sales(order pOrders, customer pCus)
        {
            InitializeComponent();
            _ordersPresenter = new OrdersPresenter();
            Orders = pOrders;
            Customer = pCus;
            LoadReport();
        }



        #region Button event

        private void buttonsArray11_btnEventAdd_Click(object sender, System.EventArgs e)
        {
            if (_formStatus == EnumFormStatus.Add)
            {
                CopyImageForOrders();
                //Orders.orders_details = OrdersDetails;
                if (_ordersPresenter.Add(Orders) > 0)
                {
                    if (Orders.Ord_Payment_Status == (int)OrdersStatus.Paid)
                    {
                        ClsMsgBox.ThongTin("Bán hàng thành công.");
                    }
                    else
                    {
                        ClsMsgBox.ThongTin("Đặt hàng thành công.");

                    }

                    ClsPublic.WriteLog(_fromCode, EnumFormStatus.Add, Orders.Ord_Code, "");
                    IsSales = true;
                    if (chkPrint.Checked)
                    {
                        rptConfirm.Print();
                    }
                    this.Close();
                }
            }

        }

        private void buttonsArray1_btnEventUpdate_click(object sender, System.EventArgs e)
        {
            if (_formStatus == EnumFormStatus.Modify)
            {
                if (_ordersPresenter.Update(Orders.orders_details.ToList(), Orders) > 0)
                {
                    ClsMsgBox.ThongTin("Cập nhật thành công.");
                    ClsPublic.WriteLog(_fromCode, EnumFormStatus.Modify, Orders.Ord_Code, "");
                    IsSales = true;
                    this.Close();
                }
            }
        }

        private void buttonsArray11_btnEventClose_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

        private void chkPrint_CheckStateChanged(object sender, System.EventArgs e)
        {

        }

        #endregion


        #region Method

        public void LoadReport()
        {
            rptConfirm = new rptConfirmSales();

            printControl1.PrintingSystem = rptConfirm.PrintingSystem;
            SetValue_Parameter();
            rptConfirm.CreateDocument(true);
            printControl1.SetDocumentMapVisibility(true);
        }

        private void SetValue_Parameter()
        {
            rptConfirm.DataSource = rptConfirm.ConvertListToDataSet(Orders.orders_details.ToList());
            rptConfirm.DisplayName = Orders.Ord_Code;

            rptConfirm.pOrderCode.Value = Orders.Ord_Code;
            rptConfirm.pDate.Value = Orders.Ord_Date_Orders;
            rptConfirm.paraCash.Value = Orders.Ord_Cash;
            rptConfirm.paraCharge.Value = Orders.Ord_Change;
            rptConfirm.paraDiscount.Value = Orders.Ord_TotalDiscount;
            rptConfirm.pDeposit.Value = Orders.Ord_Deposit;
            //con tien dat coc
            rptConfirm.pAmount.Value = Orders.Ord_Total_Amount;
            rptConfirm.pCusName.Value = Customer.Cus_FullName;
            rptConfirm.pCusAddress.Value = Customer.Cus_Address;
            rptConfirm.pCusPhone.Value = Customer.Cus_Phone;
            rptConfirm.pCusType.Value = Customer.customer_type.CusT_Name;
        }

        //lấy đường dẫn thư muc HSLT_<HSLT_ID>
        string GetDirectoryByOrderCode(string pHoaDonCode)
        {
            return Path.Combine(urlImageDes, "HoaDon", pHoaDonCode);
        }

        //thay đổi tên file hình theo HSLT_ID
        string changeFileNameImage(string pHoaDonCode, string pProductDetailId, string _extension)
        {
            string fileNameNew = pProductDetailId + "_";
            string urltemp;
            int i = 0;
            do
            {
                i++;
                urltemp = Path.Combine(GetDirectoryByOrderCode(pHoaDonCode), pProductDetailId + i + _extension);
            }
            while (File.Exists(urltemp));

            return fileNameNew + i + _extension;
        }

        private void CopyImageForOrders()
        {
            string direction = GetDirectoryByOrderCode(Orders.Ord_Code);

            if (Orders.Ord_IsOrder != null && Orders.Ord_IsOrder == 1)
            {
                if (!Directory.Exists(direction))
                    Directory.CreateDirectory(direction);
                else
                {
                    Directory.Delete(direction);
                    Directory.CreateDirectory(direction);
                }
                List<orders_details> OrdersDetails = Orders.orders_details.ToList();
                for (int i = 0; i < OrdersDetails.Count; i++)
                {
                    string fileExtension = Path.GetExtension(OrdersDetails[i].Ords_IsOrders_Image);
                    string fileName = changeFileNameImage(Orders.Ord_Code, OrdersDetails[i].Ords_PRODUCT_DETAILS_ID, fileExtension);
                    string urlImage = Path.Combine(direction, fileName);
                    try
                    {
                        File.Copy(OrdersDetails[i].Ords_IsOrders_Image, urlImage);
                    }
                    catch
                    {
                    }

                    OrdersDetails[i].Ords_IsOrders_Image = fileName;
                }
            }
        }

        #endregion



        #region Property

        public order Orders
        {
            get;
            set;
        }

        public customer Customer
        {
            get;
            set;
        }

        public bool IsSales { get; set; }

        public ISumen.EnumFormStatus FormStatus
        {
            get { return _formStatus; }
            set
            {
                _formStatus = value;
                if (_formStatus == EnumFormStatus.Add)
                {
                    buttonsArray1.btnSua.Enabled = false;
                    buttonsArray1.btnThem.Enabled = true;
                }
                else if (_formStatus == EnumFormStatus.Modify)
                {
                    buttonsArray1.btnSua.Enabled = true;
                    buttonsArray1.btnThem.Enabled = false;
                }
                else if (_formStatus == EnumFormStatus.Print)
                {
                    buttonsArray1.btnSua.Enabled = false;
                    buttonsArray1.btnThem.Enabled = false;
                }
            }
        }

        #endregion





    }
}